﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System.Windows.Threading;
using Microsoft.Phone.Shell;

namespace PanoramaApp1
{
    public partial class SidaSpelare : PhoneApplicationPage
    {
        public SidaSpelare()
        {
            InitializeComponent();
            //MediaPlayerLauncher mpl = new MediaPlayerLauncher();
            StartaSpelare();
            this.Loaded += new RoutedEventHandler(MainPage_Loaded);
        }

        private void StartaSpelare()
        {
          //  mediaElement1.Source = App.ViewModel.Player.Source;

        }

        private void btnPause_Click(object sender, RoutedEventArgs e)
        {
          //  mediaElement1.Pause();
        }

        private void btnPlay_Click(object sender, RoutedEventArgs e)
        {
           // mediaElement1.Play();
        }

        private void btnRw_Hold(object sender, GestureEventArgs e)
        {
            //bool ispressed = true;
            //while (ispressed)
            //{
            //    mediaElement1.Position -= new TimeSpan(1000);
            //    ispressed = btnRw.IsPressed;
            //}
            
            Button butt = (Button)sender;
            bool test = butt.IsPressed;
            while (test)
            {
                test = butt.IsPressed;

            }
            //    mediaElement1.Play();
        }

        private void btnFw_Click(object sender, RoutedEventArgs e)
        {
            //if (btnFw.IsPressed)
            //    mediaElement1.Position -= new TimeSpan(1000);
            //else
            //    mediaElement1.Play();
        }



        DispatcherTimer currentPosition = new DispatcherTimer(); 

        void MainPage_Loaded(object sender, RoutedEventArgs e)
    {
        myMediaElement.MediaOpened += new RoutedEventHandler(myMediaElement_MediaOpened);
        myMediaElement.MediaEnded += new RoutedEventHandler(myMediaElement_MediaEnded);
        myMediaElement.CurrentStateChanged += new RoutedEventHandler(myMediaElement_CurrentStateChanged);
        currentPosition.Tick += new EventHandler(currentPosition_Tick);

        myMediaElement.Source = App.ViewModel.Player.Source;

        //myMediaElement.Source = new Uri("http://ecn.channel9.msdn.com/o9/ch9/4807/574807/ISWPE05SLToolKitForWP_ch9.wmv", UriKind.Absolute);
    }


        void myMediaElement_CurrentStateChanged(object sender, RoutedEventArgs e)
    {
        if (myMediaElement.CurrentState == MediaElementState.Playing)
        {
            currentPosition.Start();
            ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = false; // play
            ((ApplicationBarIconButton)ApplicationBar.Buttons[1]).IsEnabled = true;  // pause
            ((ApplicationBarIconButton)ApplicationBar.Buttons[2]).IsEnabled = true;  // stop
            ((ApplicationBarIconButton)ApplicationBar.Buttons[3]).IsEnabled = true;  // ff

        }
        else if (myMediaElement.CurrentState == MediaElementState.Paused)
        {
            currentPosition.Stop();
            ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = true; // play
            ((ApplicationBarIconButton)ApplicationBar.Buttons[1]).IsEnabled = false;  // pause
            ((ApplicationBarIconButton)ApplicationBar.Buttons[2]).IsEnabled = true;  // stop
            ((ApplicationBarIconButton)ApplicationBar.Buttons[3]).IsEnabled = true;  // ff

        }
        else
        {
            currentPosition.Stop();
            ((ApplicationBarIconButton)ApplicationBar.Buttons[0]).IsEnabled = true; // play
            ((ApplicationBarIconButton)ApplicationBar.Buttons[1]).IsEnabled = false;  // pause
            ((ApplicationBarIconButton)ApplicationBar.Buttons[2]).IsEnabled = false;  // stop
            ((ApplicationBarIconButton)ApplicationBar.Buttons[3]).IsEnabled = false;  // ff
        }
    }
 
    void myMediaElement_MediaEnded(object sender, RoutedEventArgs e)
    {
        myMediaElement.Stop();
    }
 
    void myMediaElement_MediaOpened(object sender, RoutedEventArgs e)
    {
        pbVideo.Maximum = (int)myMediaElement.NaturalDuration.TimeSpan.TotalMilliseconds;
        myMediaElement.Play();
    }
 
    void currentPosition_Tick(object sender, EventArgs e)
    {
        pbVideo.Value = (int)myMediaElement.Position.TotalMilliseconds;
    }
 
    private void Play_Click(object sender, EventArgs e)
    {
        myMediaElement.Play();
    }
 
    private void Pause_Click(object sender, EventArgs e)
    {
        myMediaElement.Pause();
    }
 
    private void Stop_Click(object sender, EventArgs e)
    {
        myMediaElement.Stop();
    }

    private void FF_Click(object sender, EventArgs e)
    {
        myMediaElement.Position += new TimeSpan(5000);
    }
 
        }
    }

